Module# 12: Java IO Streams and File                                    Lecture#43: IO with Byte Streams

 

//   Example 43.1: Reading String, Integer and Double from Keyboard

 

import java.io.*;

 

public class KeyboardReading{

    public static void main(String args[]) throws IOException {

      DataInputStream dis = new DataInputStream(System.in);

     System.out.println("Enter a String: ");

      String str1 = dis.readLine();

      System.out.println("Entered String value is: " + str1);

      System.out.println("Enter a whole number: ");

      String str2 = dis.readLine();

      int x = Integer.parseInt(str2);

      System.out.println("Enter a double value: ");

      String str3 = dis.readLine();

      double y = Double.parseDouble(str3);

      if(x > y)

          System.out.println("First number " +x + " is greater than second number " + y);

          else

              System.out.println("First number " +x + " is less than second number " + y);

          dis.close();

     }

}

 

 

//  Example 43.2: Calculator using DataInputStream

 

import java.io.*;

 

class InterestCalculator   {

   public static void main(String args[ ] )   {

   Float principalAmount = new Float(0);

   Float rateOfInterest = new Float(0);

   int numberOfYears = 0;

  

   DataInputStream in = new DataInputStream(System.in);

   String tempString; 

   System.out.print("Enter Principal Amount: ");

   System.out.flush();

   tempString = in.readLine();

   principalAmount = Float.valueOf(tempString);

   System.out.print("Enter Rate of Interest: ");

 

  System.out.flush();

  tempString = in.readLine();

  rateOfInterest = Float.valueOf(tempString);

  System.out.print("Enter Number of Years:");

  System.out.flush();  

  tempString = in.readLine();

  numberOfYears =   Integer.parseInt(tempString);

  // Input is over: calculate the interest

  int interestTotal =   principalAmount*rateOfInterest*numberOfYears;

  System.out.println("Total Interest = " +   interestTotal);

    }

 }

 

//  Example 43.3: Reading bytes from a file and display data

 

// Read the file address from the Command Line

 

import java.io.*;

 

class ReadBytesDemo {

   public static void main (String args[])  {

  FileInputStream infile = null; // Create an input file stream

  int b;

  try  {

     infile = new FileInputStream(args[0]);

    // Connect infile stream to the required file

     while((b = infile.read()) != -1)  {

             System.out.print((char)b); // Read and display data

     }

     infile.close();

  }

  catch(IOException ioe)  {

    System.out.println(ioe);

  }

   }

}

 

class InputStreamTest   {

 public static void main (String args [ ] ) {

  int size;

 

  // To open a file input stream.

  FileInputStream fin;

  fin = new FileInputStream (" C:\WINDOWS\SYSTEM\SYSTEM.INI");

  size = fin.available( ); 

 

  // returns the number of bytes available

  System.out.println("Total bytes ::" + size);

  System.out.println ( " First ¼ is displayed : Using read( )");

 

  for (int i = 0; i < size /4 ; i++ ) {

      System.out.println ((char) fin.read( ) );

  }

 

//  Example 43.4: Reading a file using FileInputStream

 

class InputStreamTest   {

 public static void main (String args [ ] ) {

  int size;

 

  // To open a file input stream.

  FileInputStream fin;

  fin = new FileInputStream (" C:\WINDOWS\SYSTEM\SYSTEM.INI");

  size = fin.available( ); 

 

  // returns the number of bytes available

  System.out.println("Total bytes ::" + size);

  System.out.println ( " First ¼ is displayed : Using read( )");

 

  for (int i = 0; i < size /4 ; i++ ) {

      System.out.println ((char) fin.read( ) );

  }

    System.out.println (" Remaining bytes :" + fin.available( ) );

    System.out.println ("Next ¼ is displayed : Using read( b[ ])");

    byte b[] = new byte[size/4];

    if (fin.read (b) != b.length )

           System.err.println ("File reading error : ");

    else {

      String temp = new String (b, 0, 0, b.length ); 

      // Convert the bytes into string

      System.out.println (temp) ;   

      // display text string.

      System.out.println (" Still available:"+fin.available( ) );

      System.out.println (" skipping ¼ : Using skip ( )" );

      fin.skip(size/4);

      System.out.println (" File remaining for read ::"+fin.available( ) );

    } 

    fin.close ( );   // Close the input stream

  }

}